<?php
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();

$this->title = $goodsListTitle ? $goodsListTitle : $site_name . '-商品列表';
?>


<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/goods-list.css"/>

<script type="application/javascript">
    function href_page(page) {

        var href = window.location.href;
        if (page > 0) {
            href = setUrlParam(href, 'page', page);
        }
        window.location.href = href;

    }


    function prePage() {
        if (currentPage != null && currentPage != '' && currentPage > 1) {
            currentPage--;
            href_page(currentPage);
        }
    }
    var currentPage =<?=$page?>;
    var max_page =<?=$maxPage?>;
    function nextPage() {
        if (currentPage != null && currentPage != '' && currentPage < max_page) {
            currentPage++;
            href_page(currentPage);
        }
    }
    var currentSort =<?=$sort?>;
    function setSortType(sort) {
        var href = window.location.href;
        href = setUrlParam(href, 'sort', sort);
        href = setUrlParam(href, 'page', 1);
        window.location.href = href;
    }
    function click_default_order() {
        if (currentSort != 0) {
            setSortType(0);
        }
    }
    function click_new_order() {
        if (currentSort != 3) {
            setSortType(3);
        }
    }
    function click_price_order() {
        var sort = 1;
        if (currentSort == 1) {
            sort = 2;
        }
        setSortType(sort);
    }
</script>
<style type="text/css">
    .back-top {
        background-image: url('<?= $web ?>/images/mall-m/index-icon-top.png');
    }
</style>

<div class="main">
    <div class="header">
        <a class="search" href="<?= Url::to(['mall-m/search', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
            <img src="<?= $web ?>/images/mall-m/index-icon-search.png">
            <label>搜索商城商品</label>
        </a>
    </div>
    <div class="list">
        <div class="list-ul">
            <!--toggle order-->
            <div id="order_default" class="list-li list-cur order_btn" onclick="click_default_order()">默认</div>
            <div class="f-l long-string"></div>
            <!--            <div id="sales" class="list-li">销量</div>-->
            <!--            <div class="f-l long-string"></div>-->
            <div id="order_new" class="list-li order_btn" onclick="click_new_order()">最新</div>
            <div class="f-l long-string"></div>
            <div id="order_price" class="cost list-li order_btn" onclick="click_price_order()">价格
                <div class="arrow">
                    <img class="normal" src="<?= $web ?>/images/mall-m/goods-list-sort-normal.png"/>
                    <img class="up" style="display: none;" src="<?= $web ?>/images/mall-m/goods-list-sort-up.png"/>
                    <img class="down" style="display: none;" src="<?= $web ?>/images/mall-m/goods-list-sort-down.png"/>
                </div>
            </div>
            <!--toggle list-->
            <div class="f-l long-string"></div>
            <div id="arrange" class="list-li arrange">
                <img class="arrangement-1" src="<?= $web ?>/images/mall-m/arrangement-1.png" style="display: none;"/>
                <img class="arrangement-2" src="<?= $web ?>/images/mall-m/arrangement-2.png" />
            </div>
        </div>
    </div>
    <?php
    if (count($items) > 0) {
        ?>
        <div class="paging">
            <a href="javascript:void(0);" onclick="prePage()">
                <div class="prev f-l">
                    <p class="color-cur"><span><</span>上一页</p>
                </div>
            </a>
            <div class="pagination f-l">
                <p class="color-cur"><span><?= $page ?></span>/<span><?= $maxPage ?></span></p>
            </div>
            <a href="javascript:void(0);" onclick="nextPage()">
                <div class="next f-l">
                    <p class="color-cur">下一页<span>></span></p>
                </div>
            </a>
        </div>
        <div class="content-1" style="display: none;">
            <?php
            for ($i = 0; $i < count($items); $i++) {
                ?>
                <a href="goods-detail?goods_code=<?= $items[$i]['code'] ?>">
                    <div class="content-list">
                        <div class="content-img f-l">
                            <img src="<?= $items[$i]['goods_image'] ?>@150w"/>
                        </div>
                        <div class="content-title f-r">
                            <div class="title">
                                <p><?= $items[$i]['name'] ?></p>

                            </div>
                            <div class="contener">
                                <p class="integral f-l"><?= $integral_unit ?>：
                                    <span><?= intval($items[$i]['selling_integral']) ?></span>
                                </p>
                                <?php
                                    if($items[$i]['is_activity']=='1'){
                                ?>
                                <p class="integral-1 f-l">原价：
                                    <span><s><?= intval($items[$i]['old_price']) ?><?= $integral_unit ?></s></span>
                                </p>
                                <?php
                                    }
                                ?>

                            </div>
                        </div>
                    </div>
                </a>
                <?php
            }
            ?>
        </div>
        <div class="content-2" >
            <div class="goods-grid-frame">
                <?php
                for ($i = 0; $i < count($items); $i++) {
                    ?>
                    <a class="goods-item" href="goods-detail?goods_code=<?= $items[$i]['code'] ?>">
                        <img src="<?= $items[$i]['goods_image'] ?>@300w">
                        <label class="name"><?= $items[$i]['name'] ?></label>
                        <label class="price"><?= $integral_unit ?>：<?= number_format($items[$i]['selling_integral']) ?></label>
                    </a>
                    <?php
                }
                ?>
            </div>
        </div>
        <div class="paging">
            <a href="javascript:void(0);" onclick="prePage()">
                <div class="prev f-l">
                    <p><span><</span>上一页</p>
                </div>
            </a>
            <div class="pagination f-l">
                <p class="color-cur"><span><?= $page ?></span>/<span><?= $maxPage ?></span></p>
            </div>
            <a href="javascript:void(0);" onclick="nextPage()">
                <div class="next f-l">
                    <p class="color-cur">下一页<span>></span></p>
                </div>
            </a>
        </div>
        <?php
    } else {
        ?>
        <div class="page-prompt-5">
            <img class="page-prompt" src="<?= $web ?>/images/mall-m/page-prompt-6.jpg"/>
            <p class="title">抱歉，没有找到符合条件的商品</p>
        </div>
        <?php
    }
    ?>

    <div class="tab-frame-margin-bottom"></div>
    <div class="tab-frame">
        <a class="tab-item" href="<?= Url::to(['mall-m/index', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
            <div class="icon icon-home"></div>
            <label>首页</label>
        </a>
        <a class="tab-item" href="<?= Url::to(['mall-m/category', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
            <div class="icon icon-category"></div>
            <label>分类</label>
        </a>
        <a class="tab-item" href="<?= Url::to(['mall-m/cart', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
            <div class="icon icon-cart"></div>
            <label>购物车</label>
        </a>
        <a class="tab-item" href="user-center">
            <div class="icon icon-user"></div>
            <label>我的</label>
        </a>
    </div>
    <div class="cl-fl"></div>
    <div class="back-top"></div>
    <script type="text/javascript">
        $(function () {
            //页面名称   微信中会是乱码。。。
//            var goodsListTitle=getUrlParamValue(window.location.href,'goodsListTitle');
//            if(goodsListTitle){
//                $(document).attr("title", goodsListTitle);
//            }

            $(document).scroll(function () {
                var scrollTop = $(document).scrollTop();
                if (scrollTop > 500) {
                    $('.back-top').show(500);
                } else {
                    $('.back-top').hide(500);
                }
            });

            $('.back-top').click(function () {
                $('body,html').animate({scrollTop: 0}, 200);
            });
            //列表页切换
            $('#arrange').click(function () {
                $('.arrangement-1').toggle();
                $('.content-1').toggle();

                $('.arrangement-2').toggle();
                $('.content-2').toggle();

                //记录分类code
                if (myStorage) {
                    var arrangement_status = 0;
                    if ($('.arrangement-2').is(':visible')) {
                        arrangement_status = 1;//网格列表
                    }
                    myStorage.goods_list_arrangement_status = arrangement_status;
                }
            });
            if (myStorage) {
                var arrangement_status = myStorage.goods_list_arrangement_status;
                if (arrangement_status) {
                    if (arrangement_status == 1) {
                        $('.arrangement-1').hide();
                        $('.content-1').hide();

                        $('.arrangement-2').show();
                        $('.content-2').show();
                    }
                    if (arrangement_status == 0) {
                        $('.arrangement-1').show();
                        $('.content-1').show();

                        $('.arrangement-2').hide();
                        $('.content-2').hide();
                    }
                }
            }
            //设置排序显示
            $('.order_btn').removeClass('list-cur');
            if (currentSort == 0) {
                $('#order_default').addClass('list-cur');
            }
            if (currentSort == 3) {
                $('#order_new').addClass('list-cur');
            }
            //order_price
            if (currentSort == 1) {
                $('#order_price').addClass('list-cur');
                $('#order_price').find('.arrow > img').hide();
                $('#order_price').find('.arrow > .up').show();
            }
            if (currentSort == 2) {
                $('#order_price').addClass('list-cur');
                $('#order_price').find('.arrow > img').hide();
                $('#order_price').find('.arrow > .down').show();
            }
            //设置翻页按钮显示
            $('.prev p').addClass('color-cur');
            $('.next p').addClass('color-cur');
            if (currentPage <= 1) {
                $('.prev p').removeClass('color-cur');
            }
            if (currentPage >= max_page) {
                $('.next p').removeClass('color-cur');
            }
        });

    </script>
</div>
